import re
from string import lower
from django.core.urlresolvers import reverse, resolve
from django.http import HttpResponseRedirect, Http404
from location.models import State, City, Suburb, Street
from search.utils import get_or_or_query, is_empty

GET_PARAM_QUERY = 'q'

def search(request):
    if (GET_PARAM_QUERY in request.GET) and request.GET[GET_PARAM_QUERY].strip():
        query_string = request.GET[GET_PARAM_QUERY]

    states = []
    state_query = get_or_or_query(query_string, ['name', 'abbreviation'])
    try:
        states = State.objects.filter(state_query).values('name', 'abbreviation', 'slug').distinct()
    except :
        pass
    for state in states:
        query_string = re.sub(r'\b(%s|%s|%s|%s)\b' %
                              (state['abbreviation'], lower(state['abbreviation']),state['name'], lower(state['name'])),
                              '', query_string)

    cities = []
    city_query = get_or_or_query(query_string, ['name', 'abbreviation'])
    try:
        cities  = City.objects.filter(city_query).values('name', 'abbreviation', 'slug').distinct()
    except :
        pass
    for city in cities:
        query_string = re.sub(r'\b(%s|%s|%s|%s)\b' %
                              (city['name'], lower(city['name']), city['abbreviation'], lower(city['abbreviation'])),
                              '', query_string)

    postcodes = []
    postcode_query = get_or_or_query(query_string, ['postcode'])
    try:
        postcodes = Suburb.objects.filter(postcode_query).values('postcode').distinct()
    except :
        pass
    for postcode in postcodes:
        query_string = re.sub(r'\b%s\b' % postcode['postcode'], '', query_string)

    suburbs = []
    suburb_query = get_or_or_query(query_string, ['name'])
    try:
        suburbs = Suburb.objects.filter(suburb_query).values('name', 'slug').distinct()
    except :
        pass
    for suburb in suburbs:
        query_string = re.sub(r'\b%s\b' % suburb['name'], '', query_string)

    streets = []
    street_query   = get_or_or_query(query_string, ['name', 'extension'])
    try:
        streets = Street.objects.filter(street_query).values('name', 'extension','slug').distinct()
    except :
        pass
    for street in streets:
        query_string = re.sub(r'\b(%s|%s|%s|%s)\b' %
                              (street['name'], lower(street['name']), street['extension'], lower(street['extension'])),
                              '', query_string)

    url = resolve_url(states, cities, suburbs, postcodes, streets)

    return HttpResponseRedirect(url)

def suggest(request):
    return False;


def resolve_url(states, cities, suburbs, postcodes, streets):

    if not is_empty(streets):
        return reverse('street', args=['property', streets[0]['slug']])
    if not is_empty(suburbs):
        return reverse('suburb', args=['property', states[0]['slug']])
    if not is_empty(postcodes):
        return reverse('postcode', args=['property', postcodes[0]['postcode']])
    if not is_empty(cities):
        return reverse('city', args=['property', cities[0]['slug']])
    if not is_empty(states):
        return reverse('state', args=['property', states[0]['slug']])

    raise Http404


